package zengyan.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import zengyan.view.AnimCheckBox;

public class ViewHolder {
	
	
	private final SparseArray<View> mViews;
	
	private View mConvertview;
	
	private ViewHolder(Context context, ViewGroup parent,int layoutId,int position){
		this.mViews=new SparseArray<View>();
		mConvertview=LayoutInflater.from(context).inflate(layoutId, parent,false);
		mConvertview.setTag(this);
	}
	
	
	public static ViewHolder get(Context context,View convertview,ViewGroup parent,
														int layoutId,int position){
		if (convertview==null) {
			return new ViewHolder(context,parent,layoutId,position);
		}else {
			return (ViewHolder) convertview.getTag();
		}
	
	}
	
	public <T extends View>T getView(int viewId){
		
		View view=mViews.get(viewId);
		if (view==null) {
			view=mConvertview.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return (T) view;
	}
	
	  public View getConvertView() 
    {  
        return mConvertview;  
    }

	public AnimCheckBox setOnclick(int checkId,Boolean b) {
		AnimCheckBox c = getView(checkId);
		c.setClickable(b);
		return c;
	}

	  
	  

	    public TextView setText(int viewId, String text)  
	    {  
	        TextView view = getView(viewId);  
	        view.setText(text);  
	        return view;  
	    }  
	    
	    public Button getBtn(int viewId)
	    {
	    	Button btn=getView(viewId);
	    	return btn;
	    }
	    
	    
	    public ViewHolder setbtntext(final Context conext,int viewId,final String text){
	    	Button btn=getView(viewId);
	    	btn.setText(text);
	    	btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Toast.makeText(conext,text, Toast.LENGTH_SHORT).show();
				}
			});
	    	return this;
	    	
	    	
	    }
	  
	    
	

}
